
//商品路由

var express = require('express');
var router = express.Router();
const commodityService = require("../src/services/commodityService")
/* GET users listing. */

// 不传参数，获取1-5信息
router.get('/',async function(req, res, next) {
      let {type,value,curPage,pageSize} = req.query;
      let searchParams = {};
      if(type){
        searchParams[type] = {$regex:value};
      }
      let data = await commodityService.getCommodity({curPage:curPage || 1,pageSize:pageSize || 5},searchParams);
      res.json(data);
});
router.get('/:_id',async function(req,res){
  let {_id} = req.params;
  let data = await commodityService.findById(_id);
  res.json(data);
});
router.delete('/:_id',async function(req, res, next) {
    let {_id} = req.params;
    let result = await commodityService.deleteCommodityById(_id)
    res.json(result)
});
router.put('/:_id',async function(req, res, next) {
  let {_id} = req.params;
  let result = await commodityService.updateCommodityById({_id,...req.body})
  res.json(result)
});
router.post('/',async function(req,res){
   let data = await commodityService.insertCommodity(req.body);
   res.send(data);
});




module.exports = router;